﻿@using TbShop.Web.Helpers;
@using TbShop;
@{
    ViewBag.Title = "日志管理";
    ViewBag.ModuleId = "OperationLog";
    var operationType = ViewBag.OperationType;
    var operation = Request.QueryString["operation"];
    var uname = Request.QueryString["uname"];
    var begin = Request.QueryString["begin"];
    var end = Request.QueryString["end"];
    var isDel = AuthorizationHelper.IsAuthorised(Constants.AuthorityIdentity.DeleteOperationLog);
}
@Html.AntiForgeryToken()
<input type="hidden" name="hbegin" value="@begin" />
<input type="hidden" name="hend" value="@end" />
<input type="hidden" name="hoperation" value="@operation" />
<input type="hidden" name="huname" value="@uname" />
<div class="panel panel-default">
    <div class="panel-heading">
        <strong>日志管理</strong>
        <div class="pull-right">
            @if (isDel)
            {
                <a href="javascript:void(0);" onclick="deleteData();"><i class="glyphicon glyphicon-trash">清空</i></a>
            }
        </div>
    </div>
    <div class="panel-body">
        <nav class="navbar navbar-default">
            <div class="container-fluid">
                <div class="collapse navbar-collapse">
                    <form class="navbar-form navbar-left" method="get" id="searchForm">
                        <label>操作时间段：</label>
                        <input size="16" type="text" id="datetimeStart" readonly class="form-control"
                               value="@(string.IsNullOrWhiteSpace(begin) ? "" : begin)" name="begin">
                        --
                        <input size="16" type="text" id="datetimeEnd" readonly class="form-control"
                               value="@(string.IsNullOrWhiteSpace(end) ? "" : end)" name="end">&nbsp;
                        <label for="phoneNum">操作行为：</label>
                        <select class="chosen-select form-control" name="operation">
                            <option value="">全部</option>
                            @foreach (var key in operationType)
                            {
                                <option value="@key.Value" @(key.Value == operation ? "selected" : "")>@key.Text</option>
                            }
                        </select>&nbsp;
                        <label>管理员名称：</label>
                        <input class="form-control" value="@uname" name="uname" id="uname" />
                        &nbsp;
                        <button type="submit" class="btn btn-primary"><i class="fa fa-search"></i> 查询</button>
                    </form>
                </div>
            </div>
        </nav>
        <table id="log_table"></table>
    </div>
</div>

@section scripts{
    @Styles.Render("~/bundles/editable-table-css")
    @Scripts.Render("~/bundles/editable-table")
    <script type="text/javascript">
    function deleteData() {
        showConfirm("确认要删除该日志？", function () {
            $.ajax({
                url: "@Url.Action("DeleteData")",
                type: "POST",
                data: AntiCSRF({
                    begin: $('input[name="begin"]').val(),
                    end: $('input[name="end"]').val(),
                    operation: $('select[name="operation"]').val(),
                    username: $('input[name="uname"]').val()
                }),
                dataType: "JSON",
                success: function (data) {
                    if (data > 0) {
                        window.location.reload();
                    }
                }
            });
        }, function () { });
    }

    var userNames = JSON.parse('@(Html.Raw(ViewBag.UserName))');
        console.log(userNames)
        $(function () {
            $('#log_table').bootstrapTable({
                columns: [{
                    title: '操作',
                    field: 'Action'
                }, {
                    title: '管理员名称',
                    field: 'UserId',
                    formatter: function (value, row, index) {
                        var find = $.grep(userNames, function (n) {
                            return n.Id == value;
                        });
                        if (find.length > 0) {
                            return find[0].Name;
                        }
                        else {
                            return "--12";
                        }
                    }
                }, {
                    title: '操作内容',
                    field: 'Content'
                }, {
                    title: '操作时间',
                    field: 'OperationTime',
                    formatter: function (value) {
                        return (new Date(value)).format("yyyy-MM-dd hh:mm");
                    }
                }
                ],
                idField: 'Id',
                pagination: true,
                sidePagination: 'server',
                url: '@Url.Action("GetLogList")',
            queryParamsType: '',
            queryParams: function (params) {
                params["begin"] = $('input[name="hbegin"]').val();
                params["end"] = $('input[name="hend"]').val();
                params["operation"] = $('input[name="hoperation"]').val();
                params["username"] = $('input[name="huname"]').val();
                return params;
            },
            locale: 'zh-CN'
        });

        $("#datetimeStart").datetimepicker({
            format: 'yyyy-mm-dd',
            minView: 'month',
            language: 'zh-CN',
            autoclose: true
        }).on("click", function () {
            $("#datetimeStart").datetimepicker("setEndDate", $("#datetimeEnd").val())
        });
        $("#datetimeEnd").datetimepicker({
            format: 'yyyy-mm-dd',
            minView: 'month',
            language: 'zh-CN',
            autoclose: true
        }).on("click", function () {
            $("#datetimeEnd").datetimepicker("setStartDate", $("#datetimeStart").val())
        });
    });
    </script>
}